<template>
	<view>
		<NavBar />

		<view class="question-num text-center">
			<text class="fs-18">第 <text class="color-green">{{index+1}}</text> 题</text>
			<text class="fs-14">共10题</text>
		</view>
		<view class="stem">
			<view class="fs-24 bold mt-70">
				{{curQuestion.name}}
			</view>
			<view class="fs-18 mt-26">
				属于以下哪种类型的垃圾？
			</view>
		</view>
		<view class="buttons">
			<button type="default" @click="lastQuestion">上一题</button>
			<button type="default" @click="nextQuestion">下一题</button>
		</view>

		<view class="options">
			<view class="fs-24 bold text-center mt-40">
				选项
			</view>
			<view class="flex flex-wrap card-list">
				<view class="card2 green" @click="selectAns('干垃圾')"
					:class="curSelected!='' && (curSelected == '干垃圾' ? 'active' : 'other')">
					<image src="../../static/gan.png" mode="heightFix"> </image> 干垃圾
				</view>
				<view class="card2 red" @click="selectAns('有害垃圾')"
					:class="curSelected!='' && (curSelected == '有害垃圾' ? 'active' : 'other')">
					<image src="../../static/youhai.png" mode="heightFix"></image> 有害垃圾
				</view>
				<view class="card2 brown" @click="selectAns('湿垃圾')"
					:class="curSelected!='' && (curSelected == '湿垃圾' ? 'active' : 'other')">
					<image src="../../static/shi.png" mode="heightFix"></image> 湿垃圾
				</view>
				<view class="card2 blue" @click="selectAns('可回收物')"
					:class="curSelected!='' && (curSelected == '可回收物' ? 'active' : 'other')">
					<image src="../../static/huishou.png" mode="heightFix"></image> 可回收物
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import questions from '@/data/questions.js'
	export default {
		data() {
			return {
				questions: questions,
				index: 0,
				ansList: [],
				curSelected: ""
			}
		},
		components: {
			NavBar
		},
		computed: {
			curQuestion() {
				return this.questions[this.index]
			}
		},
		methods: {

			lastQuestion() {
				console.log('上一题')
				if (this.index > 0) {
					this.index--
				}

			},
			nextQuestion() {
				// 如果没有选择答案，则不能进入下一页
				console.log("下一题", this.curSelected)

				if (this.curSelected) {
					if (this.index < this.questions.length - 1) {
						this.index++
					} else {

						let score = this.ansList.reduce((p, n) => {
							return p + n
						})

						// console.log(score)
						uni.redirectTo({
							url: `/pages/test/TestResult?score=${score}`
						})
					}
					this.curSelected = ''
				}

			},
			selectAns(ans) {
				console.log(ans)
				this.curSelected = ans;
				if (this.curQuestion.answer === ans) {
					this.ansList[this.index] = 1
				} else {
					this.ansList[this.index] = 0
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.question-num {
		margin-top: 50rpx;
	}


	.stem {
		width: 323*2rpx;
		height: 254*2rpx;
		border-radius: 20px;
		opacity: 1;
		background: #D8FFFA;
		box-sizing: border-box;
		border: 5px solid #000000;
		box-shadow: 0px 5px 15px 0px #82FFD7;
		margin: 38rpx auto;
		text-align: center;
	}

	.buttons {
		button {
			width: 184rpx;
			height: 74rpx;
			line-height: 74rpx;
			border-radius: 1130px;
			opacity: 1;
			background-color: #000000;
			color: #fff;
			font-size: 24rpx;
		}
	}




	.card-list {
		width: 100%;
		justify-content: space-evenly;
		margin-top: 36rpx;
	}

	.card2 {
		width: 312rpx;
		height: 160rpx;
		line-height: 160rpx;
		border-radius: 20rpx;

		color: #fff;
		margin-top: 20rpx;

		font-family: OPPOSans;
		font-size: 18px;
		font-weight: bold;
		text-align: center;
		letter-spacing: 0em;

		box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.5);

		box-sizing: border-box;

		image {
			height: 84rpx;
			vertical-align: middle;
		}


	}

	.active {
		border: 5px solid #000000 !important;
	}

	.other {
		opacity: 0.5 !important;
	}
</style>